﻿@model ProductVariantModel
@using Telerik.Web.Mvc.UI;
@using Nop.Core.Domain.Catalog;
@using Nop.Admin;
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ProductId)
@Html.Telerik().TabStrip().Name("productvariant-edit").Items(x =>
{
    x.Add().Text(T("Admin.Catalog.Products.Variants.Info").Text).Content(TabInfo().ToHtmlString()).Selected(true);
    x.Add().Text(T("Admin.Catalog.Products.Variants.TierPrices").Text).Content(TabTierPrices().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Products.Variants.ProductVariantAttributes").Text).Content(TabProductVariantAttributes().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Products.Variants.Discounts").Text).Content(TabDiscounts().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "productvariant-edit"));
})
@helper TabInfo()
    {
    @Html.Partial("_ProductVariantInfo", Model, new ViewDataDictionary())
}
@helper TabTierPrices()
    {
        if (Model.Id > 0)
        {
    @(Html.Telerik().Grid<ProductVariantModel.TierPriceModel>()
            .Name("tierprices-grid")
                .DataKeys(keys =>
                {
                    keys.Add(x => x.Id);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax()
                        .Select("TierPriceList", "ProductVariant", new { productVariantId = Model.Id })
                        .Insert("TierPriceInsert", "ProductVariant", new { productVariantId = Model.Id })
                        .Update("TierPriceUpdate", "ProductVariant")
                        .Delete("TierPriceDelete", "ProductVariant");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.CustomerRole)
                        .Width(400)
                        .Centered();
                    columns.Bound(x => x.Quantity)
                        .Width(200)
                        .Centered();
                    columns.Bound(x => x.Price1)
                        .Width(100)
                        .Format("{0:G29}");
                    columns.Command(commands =>
                    {
                        commands.Edit().Text(T("Admin.Common.Edit").Text);
                        commands.Delete().Text(T("Admin.Common.Delete").Text);
                    })
                    .Width(180);
                })
                .ToolBar(commands => commands.Insert())
                .ClientEvents(events => events.OnEdit("onTierPriceEdit"))
                .EnableCustomBinding(true))
    
    <script type="text/javascript">
        function onTierPriceEdit(e) {
            $(e.form).find('#CustomerRole').data('tDropDownList').select(function (dataItem) {
                return dataItem.Value == e.dataItem['CustomerRoleId'];
            });
        }
    </script>
        }
        else
        {
    @T("Admin.Catalog.Products.Variants.TierPrices.SaveBeforeEdit")
        }
        
}
@helper TabProductVariantAttributes()
    {
        if (Model.Id > 0)
        {
            if (Model.NumberOfAvailableProductAttributes > 0)
            {
    @Html.Telerik().TabStrip().Name("productvariantattribute-edit").Items(x =>
{
    x.Add().Text(T("Admin.Catalog.Products.Variants.ProductVariantAttributes.Attributes").Text).Content(TabAttributes().ToHtmlString()).Selected(true);
    x.Add().Text(T("Admin.Catalog.Products.Variants.ProductVariantAttributes.AttributeCombinations").Text).Content(TabAttributeCombinations().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "productvariantattribute-edit"));
})
            }
            else
            {
    @T("Admin.Catalog.Products.Variants.ProductVariantAttributes.NoAttributesAvailable")
            }
        }
        else
        {
    @T("Admin.Catalog.Products.Variants.ProductVariantAttributes.SaveBeforeEdit")
        }
}
@helper TabDiscounts()
    {
        if (Model.AvailableDiscounts != null && Model.AvailableDiscounts.Count > 0)
        {
            foreach (var discount in Model.AvailableDiscounts)
            {
    <input type="checkbox" name="SelectedDiscountIds" value="@discount.Id"
            @if (Model.SelectedDiscountIds != null && Model.SelectedDiscountIds.Contains(discount.Id))
            {
                <text>checked="checked"</text>
            }
            />@discount.Name
    <br />
            }
        }
        else
        {
    @T("Admin.Catalog.Products.Variants.Discounts.NoDiscounts")
        }
}
@helper TabAttributes()
    {
    @(Html.Telerik().Grid<ProductVariantModel.ProductVariantAttributeModel>()
                .Name("productvariantattributes-grid")
                .DataKeys(keys =>
                {
                    keys.Add(x => x.Id);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax()
                        .Select("ProductVariantAttributeList", "ProductVariant", new { productVariantId = Model.Id })
                        .Insert("ProductVariantAttributeInsert", "ProductVariant", new { productVariantId = Model.Id })
                        .Update("ProductVariantAttrbiuteUpdate", "ProductVariant")
                        .Delete("ProductVariantAttributeDelete", "ProductVariant");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.ProductAttribute)
                        .Width(150)
                        .Centered();
                    columns.Bound(x => x.TextPrompt)
                        .Width(150)
                        .Centered();
                    columns.Bound(x => x.IsRequired)
                            .Template(
                                @<img alt="" src="@Url.Content("~/Administration/Content/images/active-" + item.IsRequired.ToString().ToLowerInvariant() + ".gif")" />
                            )
                            .ClientTemplate("<img alt='' src='" + Url.Content("~/Administration/Content/images/") + "active-<#= IsRequired #>.gif' />")
                            .Centered()
                            .Width(100);
                    columns.Bound(x => x.AttributeControlType)
                        .Width(150)
                        .Centered();
                    columns.Bound(x => x.DisplayOrder1)
                        .Width(150)
                        .Centered();
                    columns.Bound(x => x.ViewEditUrl)
                        .Width(200)
                        .ClientTemplate("<a href=\"<#= ViewEditUrl #>\"><#= ViewEditText #></a>")
                        .ReadOnly()
                        .Title(T("Admin.Catalog.Products.Variants.ProductVariantAttributes.Attributes.Values").Text);
                    columns.Command(commands =>
                    {
                        commands.Edit().Text(T("Admin.Common.Edit").Text);
                        commands.Delete().Text(T("Admin.Common.Delete").Text);
                    })
                    .Width(180);
                })
                .ToolBar(commands => commands.Insert())
                .ClientEvents(events => events.OnEdit("onProductVariantAttributeEdit"))
                .EnableCustomBinding(true))
    
    <script type="text/javascript">
        function onProductVariantAttributeEdit(e) {
            $(e.form).find('#ProductAttribute').data('tDropDownList').select(function (dataItem) {
                return dataItem.Value == e.dataItem['ProductAttributeId'];
            });

            $(e.form).find('#AttributeControlType').data('tDropDownList').select(function (dataItem) {
                return dataItem.Value == e.dataItem['AttributeControlTypeId'];
            });
        }
    </script>
}
@helper TabAttributeCombinations()
{
    <div style="margin: 10px 0px 10px 0px;">
        <strong>@T("Admin.Catalog.Products.Variants.ProductVariantAttributes.AttributeCombinations.Description")</strong>
    </div>
    <div>
        @(Html.Telerik().Grid<ProductVariantModel.ProductVariantAttributeCombinationModel>()
                .Name("attributecombinations-grid")
                .DataKeys(keys =>
                {
                    keys.Add(x => x.Id);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax()
                        .Select("ProductVariantAttributeCombinationList", "ProductVariant", new { productVariantId = Model.Id })
                        .Update("ProductVariantAttrbiuteCombinationUpdate", "ProductVariant")
                        .Delete("ProductVariantAttributeCombinationDelete", "ProductVariant");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.AttributesXml)
                        .ClientTemplate("<#= AttributesXml #><br /><span style=\"color: red\"><#= Warnings #></span>")
                        .Encoded(false)
                        .ReadOnly();
                    columns.Bound(x => x.StockQuantity1)
                        .Centered();
                    columns.Bound(x => x.AllowOutOfStockOrders1)
                            .Template(
                                @<img alt="" src="@Url.Content("~/Administration/Content/images/active-" + item.AllowOutOfStockOrders1.ToString().ToLowerInvariant() + ".gif")" />
                            )
                            .ClientTemplate("<img alt='' src='" + Url.Content("~/Administration/Content/images/") + "active-<#= AllowOutOfStockOrders1 #>.gif' />")
                            .Centered();
                    columns.Bound(x => x.Sku1)
                        .Centered();
                    columns.Bound(x => x.ManufacturerPartNumber1)
                        .Centered();
                    columns.Bound(x => x.Gtin1)
                        .Centered();
                    columns.Command(commands =>
                    {
                        commands.Edit().Text(T("Admin.Common.Edit").Text);
                        commands.Delete().Text(T("Admin.Common.Delete").Text);
                    })
                    .Width(180);
                })
                .EnableCustomBinding(true))
    </div>
    <div>
        <input type="submit" id="btnAddNewCombination" name="btnAddNewCombination" value="@T("Admin.Catalog.Products.Variants.ProductVariantAttributes.AttributeCombinations.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("AddAttributeCombinationPopup", "ProductVariant", new { productVariantId = Model.Id, btnId = "btnRefreshCombinations", formId = "productvariant-form" }))', 800, 550, true); return false;" class="t-button" />
        <input type="submit" id="btnRefreshCombinations" name="btnRefreshCombinations" style="display: none" />
        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#btnRefreshCombinations').click(function () {
                                    //refresh grid
                                    var optionsGrid = $("#attributecombinations-grid");
                                    optionsGrid.data('tGrid').ajaxRequest();

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
    </div>
}